﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
namespace SQT.tracnghiem.Ajax.ASP.Class
{
    public partial class add_score : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string score = Request.Form["score"];
            int lesson = int.Parse(Request.Form["lesson"]);
            var get_exam = new TblExamController().FetchAll().Where(x => x.ExamId == lesson).Single();
            var get_semester = new TblExamGroupController().FetchAll().Where(x => x.GroupId == get_exam.ExamGroup).First();
            string[] split = score.Split(';');
            string note = "";
            note = Session["admin_id"].ToString() + " " + DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year;
            if (get_semester.Semester != 3)
            {
                for (int i = 0; i < split.Length - 1; i++)
                {
                    string[] split2 = split[i].Split('_');
                    int id = int.Parse(split2[0]);
                    string write = split2[1];
                    string grammar = split2[2];
                    string listen = split2[3];
                    string kanji = split2[4];

                    decimal? write2 = null, grammar2 = null, listen2 = null, kanji2 = null, speak2 = null;
                    if (write != "")
                        write2 = decimal.Parse(write);
                    if (grammar != "")
                        grammar2 = decimal.Parse(grammar);
                    if (listen != "")
                        listen2 = decimal.Parse(listen);
                    if (kanji != "")
                        kanji2 = decimal.Parse(kanji);

                    var get_score = new TblExamScoreController().FetchAll().Where(x => x.ScoreStudentId == id && x.ScoreExam == lesson).ToList();

                    
                    if (get_score.Count != 0)
                    {
                        new TblExamScoreController().Update(get_score[0].ScoreId, null, null, write2, grammar2, listen2, kanji2, speak2, note);
                    }
                    else
                    {
                        new TblExamScoreController().Insert(id, lesson, write2, grammar2, listen2, kanji2, speak2, note);
                    }
                }
            }else
            {
                for (int i = 0; i < split.Length - 1; i++)
                {
                    string[] split2 = split[i].Split('_');
                    int id = int.Parse(split2[0]);
                    string write = split2[1];
                    string speak = split2[2];
                    string kanji = split2[3];
                    string listen = split2[4];

                    decimal? write2 = null, grammar2 = null, listen2 = null, kanji2 = null, speak2 = null;
                    if (write != "")
                        write2 = decimal.Parse(write);
                    if (listen != "")
                        listen2 = decimal.Parse(listen);
                    if (kanji != "")
                        kanji2 = decimal.Parse(kanji);
                    if (speak != "")
                        speak2 = decimal.Parse(speak);

                    var get_score = new TblExamScoreController().FetchAll().Where(x => x.ScoreStudentId == id && x.ScoreExam == lesson).ToList();

                    if (get_score.Count != 0)
                    {
                        new TblExamScoreController().Update(get_score[0].ScoreId, null, null, write2, grammar2, listen2, kanji2, speak2,note);
                    }
                    else
                    {
                        new TblExamScoreController().Insert(id, lesson, write2, grammar2, listen2, kanji2, speak2,note);
                    }
                }
            }
        }
    }
}